package com.zyx.behavior.controller;

import com.zyx.behavior.model.UserClickLog;
import com.zyx.behavior.service.UserClickLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * REST 控制器：UserClickLog
 */
@RestController
@RequestMapping("/api/ucl")
public class UserClickLogController {

    @Autowired
    private UserClickLogService userClickLogService;

    /**
     * 获取所有用户点击日志
     */
    @GetMapping
    public ResponseEntity<List<UserClickLog>> getAllUserClickLogs() {
        List<UserClickLog> logs = userClickLogService.list();
        return ResponseEntity.ok(logs);
    }

    /**
     * 根据ID获取用户点击日志
     */
    @GetMapping("/{id}")
    public ResponseEntity<UserClickLog> getUserClickLogById(@PathVariable Long id) {
        UserClickLog log = userClickLogService.getById(id);
        return ResponseEntity.ok(log);
    }

    /**
     * 创建用户点击日志
     */
    @PostMapping
    public ResponseEntity<UserClickLog> createUserClickLog(@RequestBody UserClickLog log) {
        boolean saved = userClickLogService.save(log);
        if (saved) {
            userClickLogService.sendNormalMessage("UCL-TOPIC", "NEW", log);
        }
        return ResponseEntity.ok(log);
    }

    /**
     * 更新用户点击日志
     */
    @PutMapping("/{id}")
    public ResponseEntity<UserClickLog> updateUserClickLog(@PathVariable Long id, @RequestBody UserClickLog log) {
        log.setId(id);
        userClickLogService.updateById(log);
        return ResponseEntity.ok(log);
    }

    /**
     * 删除用户点击日志
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteUserClickLog(@PathVariable Long id) {
        userClickLogService.removeById(id);
        return ResponseEntity.noContent().build();
    }
}
